package com.gzw.dms.activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.gzw.dms.MyApplication;
import com.gzw.dms.R;
import com.gzw.dms.adapter.HomeAdapter;
import com.gzw.dms.base.BaseActivity;
import com.gzw.dms.bean.ShippingOrderInfo;
import com.gzw.dms.utils.HttpUtils;
import com.gzw.dms.utils.JsonUtil;
import com.gzw.dms.utils.SysApplication;
import com.gzw.dms.utils.T;
import com.gzw.dms.utils.UrlUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ShippingOrderActivity extends BaseActivity
        implements NavigationView.OnNavigationItemSelectedListener {
    private Toolbar toolbar;
    private DrawerLayout drawer;
    private ActionBarDrawerToggle toggle;
    private NavigationView navigationView;
    private ListView lv_dingdan;
    private List<ShippingOrderInfo> shippingOrderInfo;
    private Context mContext;
    private ProgressDialog mProgressDialog;
    private String state;
    private TextView tv_name, tv_accunt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_order);
    }

    @Override
    protected void initViews() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        navigationView = (NavigationView) findViewById(R.id.nav_view);
        lv_dingdan = (ListView) findViewById(R.id.lv_dingdan);
        View headerView = navigationView.getHeaderView(0);
        tv_name = (TextView) headerView.findViewById(R.id.nav_userName);
        tv_accunt = (TextView) headerView.findViewById(R.id.nav_accunt);
    }

    @Override
    protected void initData() {
        mContext = this;
        toolbar.setTitle("未接订单");
        setSupportActionBar(toolbar);
        toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();
        shippingOrderInfo = new ArrayList<>();
        state = "0";
        tv_name.setText(MyApplication.getUserInfo().getRealName());
        tv_accunt.setText(MyApplication.getUserInfo().getUsername());
        ShowOrderTask showOrderTask = new ShowOrderTask();
        showOrderTask.execute(state);
    }

    @Override
    protected void initEvents() {
        navigationView.setNavigationItemSelectedListener(this);
        lv_dingdan.setOnItemClickListener(new mOnItemClick());
    }

//    @Override
//    public void onBackPressed() {
//
//    }

    class mOnItemClick implements AdapterView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            ShippingOrderInfo shippingOrder = (ShippingOrderInfo) parent.getItemAtPosition(position);
            Intent mIntetn = new Intent();
            Bundle bundle = new Bundle();
            mIntetn.setClass(mContext, ShippingOrderDetailActivity.class);
            bundle.putSerializable("shippingOrder", shippingOrder);
            mIntetn.putExtra("driver_id", "");
            bundle.putString("state", state);
            mIntetn.putExtras(bundle);
            startActivityForResult(mIntetn, 1);


        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != 1) {
            return;
        }
        ShowOrderTask showOrderTask = new ShowOrderTask();
        showOrderTask.execute(state);
    }

    public class ShowOrderTask extends AsyncTask<String, Void, String> {
        //onPreExecute方法用于在执行后台任务前做一些UI操作
        @Override
        protected void onPreExecute() {
            mProgressDialog = ProgressDialog.show(mContext, null, "正在加载中...");
            mProgressDialog.setCancelable(true);
        }

        protected String doInBackground(String... params) {
            Map<String, String> map = new HashMap<>();
            map.put("driver_id", MyApplication.getUserInfo().getDriver_id());
            map.put("flag", params[0]);
            String json = JsonUtil.toJson(map);
            return HttpUtils.doPost(UrlUtils.getInstance().getJavaUrl("showOrder"), "json=" + json);
        }

        @Override
        protected void onPostExecute(final String result) {
            mProgressDialog.dismiss();
            if (isNull(result)) {
                T.showShort(mContext, "连接服务器异常");
            } else {
                if (result.indexOf("errorMsg") > 0) {
                    String errorMsg = JsonUtil.getMsg(result, "errorMsg");
                    T.showShort(mContext, errorMsg);
                } else {
                    String successMsg = JsonUtil.getMsg(result, "successMsg");
                    shippingOrderInfo = JsonUtil.getPersons(successMsg, new TypeToken<List<ShippingOrderInfo>>() {
                    }.getType());
                    HomeAdapter homeAdapter = new HomeAdapter(mContext, shippingOrderInfo, state);
                    lv_dingdan.setAdapter(homeAdapter);
                }
            }
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.nav_camera) {
            state = "0";
            toolbar.setTitle("未接订单");
        } else if (id == R.id.nav_gallery) {
            state = "1";
            toolbar.setTitle("已接订单");
        }
//        else if (id == R.id.nav_slideshow) {
//            state = "2";
//            toolbar.setTitle("电子围栏签收");
//        }
        else if (id == R.id.nav_manage) {
            state = "3";
            toolbar.setTitle("实际签收");
        }
        ShowOrderTask showOrderTask = new ShowOrderTask();
        showOrderTask.execute(state);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            if (!drawer.isDrawerOpen(GravityCompat.START)) {
                drawer.openDrawer(GravityCompat.START);
            } else {
                SysApplication.getInstance().exitApp(mContext);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
